﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace CaptureImageMergeSys.Utility
{
    public class ConfigReader
    {
        public ConfigReader(string strConfigFileName = null)
        {
            m_objXmlDocument = new XmlDocument();

            try
            {
                if (null != strConfigFileName)
                {
                    m_objXmlDocument.Load(strConfigFileName);
                    m_bLoadXmlSuccess = true;
                }
            }
            catch (XmlException)
            {
                m_bLoadXmlSuccess = false;
            }


        }
        public bool InitReadFilePath(string strConfigFileName)
        {
            try
            {
                if (null != strConfigFileName)
                {
                    m_objXmlDocument.Load(strConfigFileName);
                    m_bLoadXmlSuccess = true;
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (XmlException)
            {
                m_bLoadXmlSuccess = false;
                return false;
            }
        }
        public bool ParseIntElementValue(string strElementTaghName, ref int refResultValue)
        {

            try
            {
                XmlNodeList objXmlNodeList = m_objXmlDocument.GetElementsByTagName(strElementTaghName);
                if (objXmlNodeList == null || objXmlNodeList.Count <= 0)
                {
                    return false;
                }

                XmlNode objXmlNode = objXmlNodeList.Item(0);
                refResultValue = int.Parse(objXmlNode.InnerText);
                return true;
            }
            catch (Exception e)
            {
                return false;
            }

        }
        public bool ParseFloatElementValue(string strElementTagName, ref float refResultValue)
        {

            try
            {
                XmlNodeList objXmlNodeList = m_objXmlDocument.GetElementsByTagName(strElementTagName);
                if (objXmlNodeList == null || objXmlNodeList.Count <= 0)
                {
                    return false;
                }

                XmlNode objXmlNode = objXmlNodeList.Item(0);
                refResultValue = float.Parse(objXmlNode.InnerText);
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }
        public bool ParseStringValue(string strElementTagName, ref string refResultValue)
        {
           
            try
            {
                XmlNodeList objXmlNodeList = m_objXmlDocument.GetElementsByTagName(strElementTagName);
            if (objXmlNodeList == null || objXmlNodeList.Count <= 0)
            {
                return false;
            }

            XmlNode objXmlNode = objXmlNodeList.Item(0);
            refResultValue = objXmlNode.InnerText;
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        private XmlDocument m_objXmlDocument = null;
        private bool m_bLoadXmlSuccess;
    }
}
